<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Curriculum;
use App\Models\Classes;
use App\Models\MaterialUnit;
use App\Models\TeachingMaterial;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CurriculumController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style(
            <<<CSS
        table {
            text-align: center;
        }
CSS
        );
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $class = $user->curriculums()->getQuery();
            $two = new Curriculum($class);
        } else {
            $two = new Curriculum();
        }
        return Grid::make($two, function (Grid $grid) use ($user) {
            $with = ['class','book'];
            if ($user->is_admin == 1) {
                $with[] = 'mechanism';
            }
            $grid->model()->with($with);
            $grid->column('id')->sortable();
            $grid->column('dated', '日期');
            if ($user->is_admin == 1) {
                $grid->column('mechanism.name', '所属机构');
            }
            $grid->column('class.name', '班级名称');
            $grid->column('book_id', '教材名称')->select(function ($value) {
                $class = Classes::find($value->class_id);
                $ids = json_decode($class->teaching_material, true);
                return TeachingMaterial::whereIn('id', $ids)->get()->pluck('name', 'id')->toArray();
            });
            $grid->column('unit_id')->select(function ($value) {
                $books = TeachingMaterial::find($value->book_id);
                $unit = $books->units;
                $newUnit = [];
                foreach ($unit as $key => $item) {
                    $newUnit[$item->id] = $item->materials->name.'-unit-'.($key+1);
                }
                return $newUnit;
            });
            $grid->column('link')->select([
                1 => '单词一',
                2 => '单词二',
                3 => '视频',
                4 => '音乐',
                5 => '自然拼读',
                6 => '测试',
                7 => '绘本',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('class_id')->select(Classes::where('mechanism_id', Admin::user()->id)
                    ->pluck('name', 'id')->toArray())->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Curriculum(), function (Show $show) {
            $show->field('id');
            $show->field('dated');
            $show->field('mechanism_id');
            $show->field('class_id');
            $show->field('book_id');
            $show->field('unit_id');
            $show->field('link');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $user = Admin::user();
        return Form::make(new Curriculum(), function (Form $form) use ($user) {
            $form->display('id');
            $form->date('dated', '日期');
            if ($user->is_admin == 1) {
                $form->select('mechanism_id', '所属机构')
                    ->options(Administrator::where('is_admin', -1)
                        ->pluck('name', 'id')->toArray());
                $form->select('class_id')
                    ->options(Classes::all()->pluck('name', 'id')->toArray());
                $form->select('book_id')
                    ->options(TeachingMaterial::all()->pluck('name', 'id')->toArray())
                    ->load('unit', 'classed/select');
                $unit = MaterialUnit::all();
                $newUnit = [];
                foreach ($unit as $key => $item) {
                    $newUnit[$item->id] = $item->materials->name.'-unit-'.($key+1);
                }
                $form->select('unit_id')->options($newUnit);
            } else {
                $form->hidden('mechanism_id')->default(Admin::user()->id);
                $form->select('class_id')
                    ->options(Classes::where('mechanism_id', Admin::user()->id)
                        ->pluck('name', 'id')->toArray())->load('book_id', admin_url('classed/select'));
                $form->select('book_id');
                $unit = MaterialUnit::where('mechanism_id', Admin::user()->id)->get();
                $newUnit = [];
                foreach ($unit as $key => $item) {
                    $newUnit[$item->id] = $item->materials->name.'-unit-'.($key+1);
                }
                $form->select('unit_id')->options($newUnit);
            }

            $form->select('link')
                ->options([
                    1 => '单词一',
                    2 => '单词二',
                    3 => '视频',
                    4 => '音乐',
                    5 => '自然拼读',
                    6 => '测试',
                    7 => '绘本',
                ]);
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
